<?php

namespace Biz\service\brandPack;

use Biz\model\brand\BrandRenewPackageRights;
use Biz\model\brandRights\BrandPackageRights;
use Biz\model\brandRights\BrandPackageRightsConfig;
use Biz\model\brandRights\BrandRightsAll;
use Biz\model\brandRights\BrandRightsRecored;
use Biz\model\brandRights\BrandRightsSource;
use Biz\service\BrandRights\BrandPackageRightsConfigService;
use Biz\service\BrandRights\BrandRightsAllService;
use Biz\service\BrandRights\BrandRightsSourceService;
use Doraemon\tools\Log;

class RenewPackageRightsService
{

    public static function createPackageRenewRights($brandId, $packageRenewId, $packageId, $effectTime, $effectEndTime)
    {
        $packageRightsConf = BrandPackageRightsConfigService::getOnePackageRightsConfigs($packageId);
        if (empty($packageRightsConf)) {
            return false;
        }

        $where['data_status'] = BrandRightsSource::STATUS_NORMAL;
        $sourceRightsMap = BrandRightsSourceService::getRightsSourceMap($where);
        if (empty($sourceRightsMap)) {
            return false;
        }

        $insertData = [];
        $startTime = $effectTime;
        $endTime = $effectEndTime;
        foreach ($packageRightsConf as $rights) {
            if ($rights['status'] != BrandPackageRightsConfig::STATUS_ON) {
                continue;
            }
            if (!isset($sourceRightsMap[$rights['rights_source_id']])) {
                continue;
            }
            $rightsName = $sourceRightsMap[$rights['rights_source_id']]['rights_name'];
            $insertData[] = [
                'package_renew_id' => $packageRenewId,
                'brand_id' => $brandId,
                'rights_source_id' => $rights['rights_source_id'],
                'rights_name' => $rightsName,
                'rights_num' => $rights['rights_data'],
                'start_time' => $startTime,
                'end_time' => $endTime,
                'rights_status' => BrandRenewPackageRights::STATUS_NORMAL,
                'add_time' => date('Y-m-d H:i:s', time()),
                'update_time' => date('Y-m-d H:i:s', time()),
                'effect_status' => BrandRenewPackageRights::STATUS_EFFECTIVE_NO,
            ];
        }
        return BrandRenewPackageRights::dataMultiInsert($insertData);
    }

    /**
     * 获取续费套餐的权益
     * @param $packageRenewId
     * @return array|bool
     */
    public static function getRenewPackageRights($packageRenewId)
    {
        $where = [
            'package_renew_id' => $packageRenewId,
            'rights_status' => BrandRenewPackageRights::STATUS_NORMAL,
        ];
        return BrandRenewPackageRights::dataSelect($where);
    }

    /**
     * 续费套餐权益生效
     * @param $packageRenewId
     * @return bool
     */
    public static function effectPackageRights($brandId, $packageRenewId, $dsn)
    {
        $renewPackageRights = self::getRenewPackageRights($packageRenewId);
        if (!$renewPackageRights) {
            return false;
        }

        $brandPackageRights = BrandRightsAllService::getBrandRightsMap($brandId);

        $brandRightsAll = BrandRightsAllService::getBrandRightsAllMap($brandId);

        $transData = [];

        foreach ($renewPackageRights as $rights) {
            $index = $rights['brand_id'] . '_' . $rights['rights_source_id'];
            $rights['end_time'] = strtotime(date('Y-m-d 23:59:59', $rights['end_time']));
            if (isset($brandPackageRights[$index])) {
                $transData[$rights['rights_source_id']][BrandPackageRights::class]['updateData'] = [
                    [
                        'brand_id' => $brandId,
                        'rights_source_id' => $rights['rights_source_id'],
                        'rights_name' => $rights['rights_name'],
                        'rights_num' => $rights['rights_num'],
                        'start_time' => $rights['start_time'],
                        'end_time' => $rights['end_time'],
                        'rights_status' => BrandPackageRights::STATUS_ON,
                        'addtime' => date('Y-m-d H:i:s', time()),
                        'edittime' => date('Y-m-d H:i:s', time()),
                    ],
                    [
                        'brand_id' => $brandId,
                        'rights_source_id' => $rights['rights_source_id'],
                    ],

                ];
            } else {
                $transData[$rights['rights_source_id']][BrandPackageRights::class]['insertData'] = [
                    [
                        'brand_id' => $brandId,
                        'rights_source_id' => $rights['rights_source_id'],
                        'rights_name' => $rights['rights_name'],
                        'rights_num' => $rights['rights_num'],
                        'start_time' => $rights['start_time'],
                        'end_time' => $rights['end_time'],
                        'rights_status' => BrandPackageRights::STATUS_ON,
                        'addtime' => date('Y-m-d H:i:s', time()),
                        'edittime' => date('Y-m-d H:i:s', time()),
                    ]
                ];
            }
            if (isset($brandRightsAll[$index])) {
                $transData[$rights['rights_source_id']][BrandRightsAll::class]['updateData'] = [

                    [
                        'brand_id' => $brandId,
                        'rights_source_id' => $rights['rights_source_id'],
                        'rights_name' => $rights['rights_name'],
                        'rights_num' => $rights['rights_num'],
                        'total_num' => $rights['rights_num'],
                        'start_time' => $rights['start_time'],
                        'end_time' => $rights['end_time'],
                        'rights_status' => BrandPackageRights::STATUS_ON,
                        'addtime' => date('Y-m-d H:i:s', time()),
                        'edittime' => date('Y-m-d H:i:s', time()),
                    ],
                    [
                        'brand_id' => $brandId,
                        'rights_source_id' => $rights['rights_source_id'],
                    ],

                ];
            } else {
                $transData[$rights['rights_source_id']][BrandRightsAll::class]['insertData'] = [
                    [
                        'brand_id' => $brandId,
                        'rights_source_id' => $rights['rights_source_id'],
                        'rights_name' => $rights['rights_name'],
                        'rights_num' => $rights['rights_num'],
                        'total_num' => $rights['rights_num'],
                        'start_time' => $rights['start_time'],
                        'end_time' => $rights['end_time'],
                        'rights_status' => BrandPackageRights::STATUS_ON,
                        'addtime' => date('Y-m-d H:i:s', time()),
                        'edittime' => date('Y-m-d H:i:s', time()),
                    ]
                ];
            }
            $transData[$rights['rights_source_id']][BrandRightsRecored::class]['insertData'] = [
                [
                    'brand_id' => $brandId,
                    'rights_name' => $rights['rights_name'],
                    'rights_source_id' => $rights['rights_source_id'],
                    'recored_type' => BrandRightsRecored::RECORED_TYPE_PACKAGE,
                    'source_id' => $dsn,
                    'source_data' => json_encode([
                        'order_dsn' => $dsn,
                    ]),
                    'operate_data' => json_encode([
                        'total_num' => $rights['rights_num'],
                        'rights_num' => $rights['rights_num'],
                    ]),
                ]
            ];

        }

        foreach ($transData as $rightsSourceId => $transAction) {
            foreach ($transAction as $className => $actionData) {
                foreach ($actionData as $action => $data) {
                    $result = call_user_func_array([$className, $action], $data);
                    if (!$result) {
                        Log::debug('rights effect fail', [
                            'action' => "{$className}::{$action}",
                            'params' => $data
                        ]);
                        return false;
                    }
                }
            }
        }

        $updateData = [
            'effect_status' => BrandRenewPackageRights::STATUS_EFFECTIVE_YES,
            'effect_time' => time()
        ];
        $where = [
            'package_renew_id' => $packageRenewId,
            'brand_id' => $brandId,
            'rights_status' => BrandRenewPackageRights::STATUS_NORMAL,
            'effect_status' => BrandRenewPackageRights::STATUS_EFFECTIVE_NO,
        ];
        $result = BrandRenewPackageRights::dataUpdate($updateData, $where);

        if (!$result) {
            return false;
        }
        return true;
    }


}